<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$merchid=$this->checklogin();
//声明列表页
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($_RQ['depot_id']){
		$params['depot_id']=intval($_RQ['depot_id']);
		$return['params']['depot_id']=$_RQ['depot_id'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('goodslog',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['goodsinfo']=$item['title'].$item['leititle'];
			$item['clerktitle']=$this->pdo_getcolumn('clerk',array('id'=>$item['clerk_id']),'title');
			$item['merchname']=pdo_getcolumn('jy_supplier_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
	}
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'export'=>'export',//声明导出动作op，如无需导出功能，可不声明该字段
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'string','name'=>'clerktitle','title'=>'操作员'),
			array('type'=>'select','name'=>'depot_id','title'=>'仓库','options'=>'depots'),
			array('type'=>'string','name'=>'merchname','title'=>'商家',),
			array('type'=>'string','name'=>'goodsinfo','title'=>'商品信息',),
			array('type'=>'string','name'=>'remark','title'=>'备注',),
			array('type'=>'time','name'=>'createtime','title'=>'操作时间',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'select','width'=>15,'name'=>'depot_id','title'=>'仓库','options'=>'depots'),
		),
	);
	$return['depots']=$this->pdo_getall('depot',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
//导出动作
if($_RQ['op']=='export'){
	//整理筛选条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('goodslog',$parameter);
	if($list){
		foreach($list as &$item){
			//整理从数据库获取到的数据
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$goods=$this->pdo_get('goods',array('id'=>$item['goods_id']),array('title','productsn','unit'));
			$item=array_merge($goods,$item);
			$item['user_name']=model('core/operator')->get_username($item['uid']);
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'商品名称',
		'total'=>'入库数量',
		'unit'=>'单位',
		'createtime'=>'入库时间',
		'remark'=>'备注',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'日志列表');
}